
clear
set more off
capture log close
set seed 12345
set matsize 5000
set maxvar 20000	
* set globals
global OUTPUT "$DIR/Output"
global DATA "$DIR"	
cd "$DIR"

/* SET LOCALS */
* set earliest tax year to keep
local yrb = 2008
* set last tax year to keep
local yre = 2016
* set excluded base year for event-time and controls
local baseyr "2012"
* log or level ("ln" "level")
local rlog "ln"
local rloglab "`rlog'"


use $DATA/reg_file_firm.dta, clear
keep if year==2012

****************************************************************************************
**  Table 1 - firm summary stat
****************************************************************************************

gen n_own_passive = n_own-n_own_active
gen margin = netinc / revenue
gen wage_rev = tot_wages/revenue
replace wage_rev = . if wage_rev<0
replace wage_rev = . if wage_rev>1

gen dmtr_iv = mtr_2013 - mtr
replace dmtr_iv = 0 if dmtr_iv<0

* correct for tax liability treatment concept - net of cap gains and div
replace dmtr_iv = . if tt1112_ncg~=1 | bal_liab==0
replace dlnmtr_iv = . if tt1112_ncg~=1 | bal_liab==0
replace dlnatr_tot_iv = . if tt1112_ncg~=1 | bal_liab==0
replace datr_tot_iv = . if tt1112_ncg~=1 | bal_liab==0
replace dliab_tot_iv_2012 = . if tt1112_ncg~=1 | bal_liab==0
replace dpwliab_tot_iv_2012 = . if tt1112_ncg~=1 | bal_liab==0
replace liab_tot = . if tt1112_ncg~=1 | bal_liab==0

* Winsorize 
foreach w in 1 {
	local ptop = 100-`w'
	local pbot = `w'
	foreach o in revenue netinc valadd valaddpw_ft tot_wages margin wage_rev {
		sort year `o'
		by year: egen zt=pctile(`o'), p(`ptop')
		by year: egen zb=pctile(`o'), p(`pbot')
		gen w`ptop'_`o' = `o'
		replace w`ptop'_`o' = zt if `o'>zt & `o'~=.
		replace w`ptop'_`o' = zb if `o'<zb & `o'~=.
		drop zt zb
	}
}

**********************
**  Table 1, Panel A + Panel B
**********************
*Avgs all firms
preserve
	collapse (count) n=firm_tin (mean) w99_revenue w99_netinc w99_valadd w99_valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft w99_tot_wages margin wage_rev
	gen stat = "mean"
	gen t1 = 99
	foreach v in revenue netinc valadd valaddpw_ft tot_wages {
		ren w99_`v' `v' 
	}
	tempfile tmean
	save `tmean',replace 
restore

*median all firms
preserve
	collapse (count) n=firm_tin (median) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev 
	gen stat = "median"
	gen t1 = 99
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp50
	save `tp50',replace
restore

*p10 all firms
preserve
	collapse (count) n=firm_tin (p10) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev 
	gen stat = "p10"
	gen t1 = 99
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp10
	save `tp10',replace
restore

*p90 all firms
preserve
	collapse (count) n=firm_tin (p90) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev 
	gen stat = "p90"
	gen t1 = 99
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp90
	save `tp90',replace
restore


* Treat v. ctrl
preserve
	collapse (count) n=firm_tin (mean) w99_revenue w99_netinc w99_valadd w99_valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft w99_tot_wages margin wage_rev dlnmtr_iv dmtr_iv dlnatr_tot_iv datr_tot_iv dliab_tot_iv_2012 dpwliab_tot_iv_2012 liab_tot, by(t1)
	gen stat = "mean"
	drop if t1 == .
	foreach v in revenue netinc valadd valaddpw_ft tot_wages {
		ren w99_`v' `v' 
	}
	tempfile tmeant
	save `tmeant',replace 
restore

preserve
	collapse (count) n=firm_tin (median) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev , by(t1)
	gen stat = "median"
	drop if t1 == .
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp50t
	save `tp50t',replace
restore

preserve
	collapse (count) n=firm_tin (p10) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev , by(t1)
	gen stat = "p10"
	drop if t1 == .
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp10t
	save `tp10t',replace
restore

preserve
	collapse (count) n=firm_tin (p90) revenue netinc valadd valaddpw_ft n_ee n_fulltime n_own n_own_active n_own_passive n_own_top  wages wages_ft tot_wages margin wage_rev, by (t1)
	gen stat = "p90"
	drop if t1 == .
	foreach v in revenue netinc valadd valaddpw_ft wages wages_ft tot_wages {
		replace `v' = `v'/10
		replace `v' = round(`v')
		replace `v' = `v'*10
	}
	tempfile tp90t
	save `tp90t',replace
restore

* combine and save
preserve
	use `tmean', clear
	append using `tp50'
	append using `tp90'
	append using `tp10'
	append using `tmeant'
	append using `tp50t'
	append using `tp90t'
	append using `tp10t'
	outsheet using $OUTPUT/table1_panelA.csv, comma replace
restore



***********************************
**  Table 1, Panel C - Regression adjusted control balance
***********************************
* create netinc cat
foreach i in netinc valaddpw_ft {
	xtile `i'cat = `i',nquantiles(20)
}
local r1 "i.valaddpw_ftcat##ib`baseyr'.year i.netinccat20##ib`baseyr'.year i.fsize##ib`baseyr'.year i.naics##ib`baseyr'.year i.state##ib`baseyr'.year"
local outs " wages_ft  n_ee_lower w99_netinc w99_valaddpw_ft w99_valadd w99_tot_wages w99_revenue w99_margin"
foreach r in 1 {
	foreach o in `outs' {
		eststo `o'_r`r': quietly reg `o' t1 `r`r''
	}
}
esttab using "$OUTPUT/table1_panelC.csv", replace se ar2 mtitles( ) keep(t1) 
eststo clear



***********************************
**  Tables A.3 and A.4 - state and industry TvC
***********************************
preserve
	collapse (count) n=firm_tin, by(state t1)
	outsheet using $OUTPUT/table_A4_state.csv, comma replace
restore
preserve
	collapse (count) n=firm_tin, by(naics t1)
	outsheet using $OUTPUT/table_A3_ind.csv, comma replace
restore



	
clear
exit
exit